<?php

namespace App\Http\Controllers;


use App\Model\UserBank;
use App\Model\UserCashLog;
use App\Service\baofu\SecurityUtil\NewRsaUtil;
use App\Service\BaofuPay;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;

class AccountNotifyController
{
    public function notify(Request $request)
    {
        Log::info('account notify', $request->all());
        $baofu = App::make(BaofuPay::class); // 通过容器解析依赖
        $data = $request->input('data_content');
        $dePostString = NewRsaUtil::decrptByPub($data, $baofu->bfFilePath);
        Log::info('解密data'.json_encode($dePostString));
        if (empty($dePostString)) {
            Log::info("解密空值，请检查证书是否正常");
        }
        $dePostString = json_decode($dePostString,true);
        $type = $dePostString['serviceTp'];
//        $payment = AdaPayService::getPayment();
//        if (!$payment->ada_tools->verifySign($sign, $data)) {
//            Log::error('支付回调验签失败', [$request->all()]);
//            return 'fail';
//        }
        switch ($type) {
            case 'BCT3-3104-001-01':
                $this->notifyCash($dePostString);
                break;
            case 'BCT3-1104-001-01':
                $this->notifyMember($dePostString);
                break;
        }
        return 'OK';
    }

    /**开户回调
     * @param $data
     * @param $status
     * @return void
     */


    public function notifyCash($data){
        $data['data'] = json_decode($data['data'],true);
        $cashLog = UserCashLog::query()->where('order_no',$data['data']['transSerialNo'])->first();
        if(!$cashLog){
            Log::info('select-cash-log-error'.json_encode($data));
            return '';
        }
        $cashLog->status = 1;
        $cashLog->error_msg= json_encode($data['data']);
        if($data['data']['state'] == 3 || $data['data']['state'] == 0){
            $user = User::query()->where('id',$cashLog->user_id)->first();
            $user->amount += $cashLog->cash_amt;
            $bfAccount  = app::make(BaofuPay::class);
            $bfAccount->desAccount((float)$cashLog->cash_amt,$user);
            $user->save();
            $cashLog->status = $data['data']['state'];
        }
        $cashLog->save();
    }
    public function notifyMember($data){
        $data['data'] = json_decode($data['data'],true);
        if(!isset($data['data']['loginNo'])){
            return '';
        }
        $bank = UserBank::query()->where('order_no',$data['data']['loginNo'])->first();
        if(!$bank){
            return '';
        }
        $userInfo = User::query()->where('id',$bank->user_id)->first();
        $bank->notify_time = date('Y-m-d H:i:s',time());
        $bank->result = json_encode($data);
        $bank->save();
        if(isset($data['data']['contractNo']) && $data['data']['state'] == 1){
            $userInfo->account_id  = $bank->account_id = $data['data']['contractNo'];
            $userInfo->member_id = $data['data']['loginNo'];
            $bank->reason = '开户成功';
            $bank->status = 2;
            $userInfo->save();
        }else{
            if($data['data']['state'] == 2){
                $bank->status = 1;
                $bank->reason = '开户中';
            }else{
                $bank->status = 3;
                $bank->reason = $data['data']['errorMsg'];
            }
        }
        $bank->save();
    }
}
